package com.ydcycloud.sys.model;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

import org.apache.commons.beanutils.PropertyUtils;


/**
 * 持久化基类
 */
@MappedSuperclass
public class BasePersistentObject extends PersistentObject {

	private static final long serialVersionUID = 6531210924629425166L;

	/** 创建人 */
	private String createBy;
	/** 创建时间 */
	private Date createTime;
	/** 最后更新时间 */
	private Date lastUpdateTime;

	@Column(name = "create_by")
	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	@Column(name = "create_date")
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}


	@Column(name = "last_modify_date")
	public Date getLastUpdateTime() {
		return lastUpdateTime;
	}

	public void setLastUpdateTime(Date lastUpdateTime) {
		this.lastUpdateTime = lastUpdateTime;
	}
	

	/**
	 * 将类转化为删除createTime,lastUpdateTime,createBy属性的map结构
	 * @author
	 * @param bean
	 * @return
	 */
	public static Map<String, Object> transformToMap(Object bean) 
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		Map<String, Object> map = PropertyUtils.describe(bean);
		map.remove("createTime");
		map.remove("lastUpdateTime");
		map.remove("createBy");
		return map;	
	}
	
	/**
	 * 将bean转化为含有指定属性的map结构
	 * @author
	 * @param bean
	 * @param propertys
	 * @return
	 */
	public static Map<String, Object> transformToMap(Object bean, String[] propertys) 
			throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		Map<String, Object> map = PropertyUtils.describe(bean);
		Map<String, Object> r = new HashMap<String, Object>();
		for(int i = 0; i < propertys.length; i++) {
			r.put(propertys[i], map.get(propertys[i]));
		}
		return r;
	}
	
	public static List<Map<String, Object>> transformToMapList(List<? extends BasePersistentObject> list) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		List<Map<String, Object>> r = new ArrayList<Map<String, Object>>();
		for (Object bean : list) {
			Map<String, Object> map = transformToMap(bean);
			r.add(map);
		}
		return r;
	}
	
	public static List<Map<String, Object>> transformToMapList(List<? extends BasePersistentObject> list, String[] propertys) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
		List<Map<String, Object>> r = new ArrayList<Map<String, Object>>();
		for (Object bean : list) {
			Map<String, Object> map = transformToMap(bean, propertys);
			r.add(map);
		}
		return r;
	}
	
}